package main

import (
	"github.com/google/gopacket/pcap"
	"github.com/google/gopacket"
	"fmt"
	"github.com/google/gopacket/layers"
)

func main() {

	if handle, err := pcap.OpenOffline("src/com/packet/pcap/test.pcap"); err != nil {
		panic(err)
	} else {
		packetSource := gopacket.NewPacketSource(handle, handle.LinkType())
		i := 0
		for packet := range packetSource.Packets() {
			fmt.Println("==== index ", i)
			i = i + 1
			handlePacket(packet)
		}
	}
}
func handlePacket(packet gopacket.Packet) {

	for _, layer := range packet.Layers() {
		switch layer.(type) {
		case *layers.Ethernet:
			l := layer.(*layers.Ethernet)
			fmt.Println(l.LayerType(), l.SrcMAC, l.DstMAC)
		case *layers.IPv4:
			l := layer.(*layers.IPv4)
			fmt.Println(l.LayerType(), l.TTL, l.SrcIP, l.DstIP)
		case *layers.ICMPv4:
			l := layer.(*layers.ICMPv4)
			fmt.Println(l.TypeCode, l.Seq)
		case *layers.TCP:
			l := layer.(*layers.TCP)
			fmt.Println(l.SrcPort,l.DstPort,l.RST,l.ACK,l.SYN)
		}

	}

}
